﻿@{
    var totalRecords = ViewData.Get<long>("totalRecords", 0);
    var subject = ViewData.Get<string>("subject", "标题");
    ViewData["Title"] = new string[] { subject };
    var IsComment = ViewData.Get<bool>("IsComment", true);
    var commentedObjectId = ViewData.Get<long>("commentedObjectId", 0);
    var tenantTypeId = ViewData.Get<string>("tenantTypeId", string.Empty);
    var currentUser = UserContext.CurrentUser;
}

@Styles.Render("~/Bundle/Styles/qqFace")
<div class="tn-article-detail tn-mb-20">
    <h4>
        <a href="@SiteUrls.Instance().CMSImgDetail(commentedObjectId)">
            @HtmlUtility.TrimHtml(subject, 50)
        </a>
    </h4>
</div>
<div class="row jh-my-post">
    <div class="col-xs-9">
        <div class="jh-padding-white tn-mb-20">
            <div class="tn-title">
                <h5 class="pull-left">评论列表 <span class="commentcount">@totalRecords</span><small id="AuditCount" data-commontcount="@totalRecords" class="tn-theme-color tn-ml-10 tn-font-14"></small></h5>
            </div>
            @if (!IsComment)
            {
                if (currentUser != null)
                {
                    using (Html.BeginForm("CreateComment", "Portal", FormMethod.Post, new { @id = "createComment-0", @class = "form-horizontal tn-form-validation" }))
                    {
                    @Html.AntiForgeryToken()
                        @Html.Hidden("parentId", 0)
                        @Html.Hidden("commentedObjectId", commentedObjectId)
                        @Html.Hidden("TenantTypeId", tenantTypeId)
                        @Html.Hidden("Body", "")
                        @Html.ValidationMessage("Body")
                        <textarea name="Bodys" id="textBody" class="form-control tn-mb-15" rows="5" placeholder=""></textarea>
                        <div class="btn-toolbar" role="toolbar">
                            <div class="btn-group btn-group-sm pull-left" role="group">
                                <a class="btn btn-default emotion"  role="button"><i class="fa fa-smile-o"></i></a>
                            </div>
                            <button class="btn btn-primary btn-sm tn-btn-wp pull-right commentsubmit" data-id="0" type="button">评论</button>
                        </div>
                       
                    }
                }
                else
                {
                <div class="jh-textarea-button">
                    <textarea name="Body" id="textBody" class="form-control tn-mb-15" rows="5" style="text-align:center;" placeholder="登录后才能评论" disabled></textarea>
                    <a class="btn btn-primary tn-btn-wp" href="@SiteUrls.Instance().Login()">登录</a>
                </div>
                }
            }
        </div>
        @if (!IsComment)
        {
        <div class="jh-padding-white" id="CommentLists">
            @Html.Action("_ListComment", "Portal", new { commentedObjectId = commentedObjectId, tenantTypeId = tenantTypeId })
        </div>
        }
    </div>
    <div class="col-xs-3">
        @Html.Action("_Hotarticle", "CMS")
    </div>

</div>

<script type="text/javascript">
    require(['jquery', 'tnlayer', 'qqFace'], function ($) {
        $(function () {
            $('.emotion').qqFace({
                id: 'facebox',
                assign: 'textBody',
                ohref: '/common/GetEmotions',
                directoryname: 'default',
            });
        });
        //评论
        $(document).on('click', 'button.commentsubmit', function (e) {
            var _that = $(this);
            var id = _that.data("id");
            var tenantTypeId = $("#tenantTypeId").val();
            var commentedObjectId = $("#commentedObjectId").val();
            if ($("#createComment-" + id).find("textarea").val() == "" && $("#createComment-" + id).find("textarea").text() == "") {
                layer.msg('评论内容不能为空！', {
                    icon: 2
                })
                return false;
            }
            if ($("#createComment-" + id).find("textarea").val().length > 1000 || $("#createComment-" + id).find("textarea").text().length > 1000) {
                layer.msg('评论内容过长！', {
                    icon: 2
                })
                return false;
            }
            _that.attr("disabled", true)
            if ($("#createComment-" + id).find("textarea").text() != "") {
                $("#createComment-" + id).find("#Body").val($("#createComment-" + id).find("textarea").text())
            }
            if ($("#createComment-" + id).find("textarea").val() != "") {
                $("#createComment-" + id).find("#Body").val($("#createComment-" + id).find("textarea").val())
            }

            $.post("@CachedUrlHelper.Action("CreateComment", "portal")", $("#createComment-" + id).serialize(), function (data) {
                _that.attr("disabled", false)
                if (data.MessageType == -1) {
                    layer.msg(data.MessageContent, {
                        icon: 2
                    });
                    return false;
                }
                $("#textBody").val("");
                $("#textBody").text("");
                if (data.MessageType) {
                    layer.msg('评论成功', {
                        icon: 1
                    })
                    var parm = jQuery.param({ tenantTypeId: tenantTypeId, commentedObjectId: commentedObjectId, t: new Date().getTime() }, true);
                    $.get("@CachedUrlHelper.Action("_ListComment", "portal")", parm, function (data) {
                        $("#CommentLists").html(data)
                    });
                    $(".commentcount").text(parseInt($(".commentcount").text()) + 1);
                }
                else {
                    layer.msg('评论失败', {
                        icon: 2
                    })
                }
            });
        });

        //显示评论控件
        $(document).on('click', 'a[id^=commentcreate-]', function (e) {
            var _that = $(this);
            var _thatul = _that.parents("ul")[0];
            $(".CommentControl").remove()
            var href = _that.data("href");
            var parm = jQuery.param({ t: new Date().getTime() }, true);
            $.get(href, parm, function (data) {
                $(_thatul).html();
                $(_thatul).append(data);
            });
        });

        //删除评论
        $(document).on('click', 'a[id^=commentdelete-]', function (e) {
            var _that = $(this);
            var id = _that.attr("id").replace(/[^0-9]/ig, "");
            layer.confirm("确定删除么？", {
                btn: ['确认', '取消']
            }, function () {
                $.post("@CachedUrlHelper.Action("_DeleteComment", "Portal")", { commentId: id }, function (data) {
                    if (data.MessageType) {
                        layer.msg("删除成功", {
                            icon: 1
                        })
                        $(".commentcount").text(parseInt($(".commentcount").text()) - 1);
                        _that.parents(".media").remove()
                    }
                });
            })
        });
    })
</script>